#include <iostream>
#include <vector>
#include <stdexcept>
class Matrix {
private:
    std::vector<std::vector<int>> data;
    int rows, cols;
public:
    Matrix(int rows, int cols) : rows(rows), cols(cols) {
        data.resize(rows, std::vector<int>(cols, 0));
    }
    void initialize(const std::vector<std::vector<int>>& values) {
        if (values.size() != rows || values[0].size() != cols) {
            throw std::invalid_argument("Matrix dimensions do not match.");
        }
        data = values;
    }
    int getRows() const {
        return rows;
    }
    int getCols() const {
        return cols;
    }
    friend std::ostream& operator<<(std::ostream& os, const Matrix& matrix) {
        for (const auto& row : matrix.data) {
            for (int val : row) {
                os << val << " ";
            }
            os << std::endl;
        }
        return os;
    }
    Matrix operator+(const Matrix& other) const {
        if (rows != other.rows || cols != other.cols) {
            throw std::invalid_argument("Matrices must have the same dimensions to be added.");
        }
        Matrix result(rows, cols);
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                result.data[i][j] = data[i][j] + other.data[i][j];
            }
        }
        return result;
    }
};
int main() {
    Matrix mat1(3, 3);
    Matrix mat2(3, 3);
    std::vector<std::vector<int>> values1 = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    mat1.initialize(values1);
    std::vector<std::vector<int>> values2 = {
        {9, 8, 7},
        {6, 5, 4},
        {3, 2, 1}
    };
    mat2.initialize(values2);
    std::cout << "Matrix 1:" << std::endl;
    std::cout << mat1 << std::endl;
    std::cout << "Matrix 2:" << std::endl;
    std::cout << mat2 << std::endl;
    Matrix sum = mat1 + mat2;
    std::cout << "Sum of Matrices:" << std::endl;
    std::cout << sum << std::endl;
    return 0;
}